home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / docs / irithlp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  11.5 KB  |  409 lines

  1. /*****************************************************************************
  2. *   Documentation processor of IRIT- a 3d solid modeller.             *
  3. ******************************************************************************
  4. * Usage:                                     *
  5. *   Irit [-t] [-l] [-h] [-o OutFileName] [-z] [InFileName]             *
  6. *                                         *
  7. * Written by:  Gershon Elber                Ver 1.0, Nov. 1991   *
  8. *****************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include "irit_sm.h"
  14.  
  15. #define MAX_ARRAY_SIZE    1000
  16.  
  17. typedef enum {
  18.     UNDEFINED_DOC_PROCESS = 0,
  19.  
  20.     PLAIN_TEXT_DOCS,
  21.     LATEX_DOCS,
  22.     IRIT_HELP_DOCS
  23. } DocProcessType;
  24.  
  25. void ProcessFiles(FILE *Fin, FILE *Fout, DocProcessType OutputDocType);
  26. void FilterLatexMods(char *Line1, char *Line2);
  27.  
  28. /*****************************************************************************
  29. *   Main module - process command line optionsd modeller.             *
  30. *****************************************************************************/
  31. void main(int argc, char **argv)
  32. {
  33.     FILE
  34.     *Fin = stdin,
  35.     *Fout = stdout;
  36.     DocProcessType
  37.     OutputDocType = UNDEFINED_DOC_PROCESS;
  38.  
  39.     while (argc > 1) {
  40.     if (strcmp(argv[1], "-t") == 0) {
  41.         OutputDocType = PLAIN_TEXT_DOCS;
  42.         argc--;
  43.         argv++;
  44.     }
  45.     else if (strcmp(argv[1], "-l") == 0) {
  46.         OutputDocType = LATEX_DOCS;
  47.         argc--;
  48.         argv++;
  49.     }
  50.     else if (strcmp(argv[1], "-h") == 0) {
  51.         OutputDocType = IRIT_HELP_DOCS;
  52.         argc--;
  53.         argv++;
  54.     }
  55.     else if (strcmp(argv[1], "-o") == 0) {
  56.         if ((Fout = fopen(argv[2], "w")) == NULL) {
  57.         fprintf(stderr, "Failed to open \"%s\".\n", argv[2]);
  58.         exit(1);
  59.         }
  60.         argc -= 2;
  61.         argv += 2;
  62.     }
  63.     else if (strcmp(argv[1], "-z") == 0) {
  64.         fprintf(stderr, "Usage: IritHlp [-t] [-l] [-h] [-o OutFileName] [-z] [InFileName].\n");
  65.         exit(0);
  66.     }
  67.     else
  68.         break;
  69.     }
  70.  
  71.     if (argc > 1) {
  72.     if ((Fin = fopen(argv[1], "r")) == NULL) {
  73.         fprintf(stderr, "Failed to open \"%s\".\n", argv[2]);
  74.         exit(1);
  75.     }
  76.     }
  77.  
  78.     if (OutputDocType == UNDEFINED_DOC_PROCESS) {
  79.     fprintf(stderr, "One of [-t] [-l] [-h] must be specified.\n");
  80.     exit(1);
  81.     }
  82.  
  83.     ProcessFiles(Fin, Fout, OutputDocType);
  84.  
  85.     fclose(Fin);
  86.     fclose(Fout);
  87.  
  88.     exit(0);
  89. }
  90.  
  91. /*****************************************************************************
  92. *   Reads lines from Fin and dumps them to Fout after the following         *
  93. * processing take place:                             *
  94. * Allways:                                            *
  95. *    a. Lines start with ';' are ignored ans skipped (comments).         *
  96. *    b. Lines start with '#' followed by four ingeters (indentation,         *
  97. *       # elements per line, a Boolean to specify vertical seperator, and    *
  98. *       a Boolean flag on the row (TRUE) or col (FALSE) entries order)         *
  99. *       signifies an array. All entries, one per line are part of the array  *
  100. *       until another '#' occurs. Each of the method below should do its     *
  101. *       best to create such an array.                         *
  102. *    c. Lines start with '%' are copied verbatim to all output types.         *
  103. * 1. OutputDocType = PLAIN_TEXT_DOCS                         *
  104. *    + Lines start with '*' are echoed as is with no tex related processing. *
  105. *    + Lines start with '@' are ignored.                     *
  106. *    + Lines start with '!' are echoed as is with underline.             *
  107. *    + Lines start with '$' are ignored.                     *
  108. *    + Lines start with '&' are echoed as is with underline.             *
  109. *    + Other lines are dumped out as is after filtering out of '{\cmd * }'   *
  110. *      latex modifiers.                                 *
  111. *    + all lines are dumped out without their first character.             *
  112. * 2. OutputDocType = LATEX_DOCS                             *
  113. *    + Lines start with '*' are echoed as is with no tex related processing. *
  114. *    + Lines start with '@' are echoed as is.                     *
  115. *    + Lines start with '!' are ignored.                     *
  116. *    + Lines start with '$' are ignored.                     *
  117. *    + Lines start with '&' are ignored.                     *
  118. *    + Other lines are dumped out as is.                     *
  119. *    + All lines are dumped out without their first character.             *
  120. * 3. OutputDocType = IRIT_HELP_DOCS                         *
  121. *    + Lines start with '*' are echoed as is with no tex related processing. *
  122. *    + Lines start with '@' are ignored.                     *
  123. *    + Lines start with '!' are dumped out as is.                 *
  124. *    + Lines start with '$' causes echoing of the '$' in a single line.      *
  125. *    + Lines start with '&' are ignored.                     *
  126. *    + Other lines are dumped out as is after filtering out of '{\cmd * }'   *
  127. *      latex modifiers.                                 *
  128. *    + All lines are dumped out with their first character as in input.      *
  129. *****************************************************************************/
  130. void ProcessFiles(FILE *Fin, FILE *Fout, DocProcessType OutputDocType)
  131. {
  132.     int i, j, k,
  133.     LineNum = 0;
  134.     char Line1[LINE_LEN_LONG], Line2[LINE_LEN_LONG], *p;
  135.  
  136.     while (fgets(Line1, LINE_LEN_LONG - 1, Fin) != NULL) {
  137.     LineNum++;
  138.     if (Line1[0] == ';')
  139.         continue;
  140.  
  141.     if (Line1[0] == '%') {
  142.         if (OutputDocType != LATEX_DOCS)
  143.         fprintf(Fout, " ");
  144.         fprintf(Fout, &Line1[1]);
  145.         continue;
  146.     }
  147.         
  148.     if (Line1[0] == '#') {             /* Beginning of array definition. */
  149. #if defined(AMIGA) && defined(__SASC)
  150.         static char __far ArrayEntries[MAX_ARRAY_SIZE][LINE_LEN_LONG];
  151. #else
  152.           static char ArrayEntries[MAX_ARRAY_SIZE][LINE_LEN_LONG];
  153. #endif
  154.         int Indentation, NumPerLine, VertSep, NumLines, EntriesRowBased,
  155.         StartLineNum = LineNum,
  156.         NumArrayEntries = 0;
  157.  
  158.         if (sscanf(&Line1[1], "%d %d %d %d",
  159.                &Indentation, &NumPerLine,
  160.                &VertSep, &EntriesRowBased) != 4) {
  161.         fprintf(stderr, "Four integers expected in line %d\n", LineNum);
  162.         exit(1);
  163.         }
  164.  
  165.         /* Gets all the entries of the array: */
  166.         while ((p =    fgets(ArrayEntries[NumArrayEntries++],
  167.                         LINE_LEN_LONG - 1, Fin)) != NULL &&
  168.            p[0] != '#' &&
  169.            NumArrayEntries < MAX_ARRAY_SIZE) {
  170.         p += strlen(p);              /* Remove the EOL from string. */
  171.         while (*--p < ' ');
  172.         p[1] = 0;
  173.  
  174.         LineNum++;
  175.             }
  176.         if (p != NULL)
  177.         LineNum++;
  178.         NumArrayEntries--;
  179.  
  180.         if (p[0] != '#') {
  181.         fprintf(stderr,
  182.             "Array at line %d too large (max %d) or missing '#'.\n",
  183.             StartLineNum, MAX_ARRAY_SIZE);
  184.         exit(1);
  185.         }
  186.  
  187.         if (NumPerLine == 0) {
  188.         fprintf(stderr,
  189.             "Number of items per line is zero at line %d.\n",
  190.             StartLineNum);
  191.         exit(1);
  192.         }
  193.  
  194.         /* Now we have all the array elements. Computes how many lines   */
  195.         /* need and print the lines in the format this documentation is  */
  196.         /* suppose to be in.                         */
  197.         NumLines = (NumArrayEntries + NumPerLine - 1) / NumPerLine;
  198.         switch (OutputDocType) {
  199.         case PLAIN_TEXT_DOCS:
  200.             sprintf(Line2, "%%-%ds", Indentation);
  201.             fprintf(Fout, "\n");
  202.             for (i = 0; i < NumLines; i++) {
  203.             k = EntriesRowBased ? i * NumPerLine : i;
  204.             fprintf(Fout, "   ");
  205.             for (j = 0; j < NumPerLine; j++) {
  206.                 if (k >= NumArrayEntries)
  207.                 fprintf(Fout, Line2, "");
  208.                 else {
  209.                 FilterLatexMods(ArrayEntries[k],
  210.                         ArrayEntries[k]);
  211.                 fprintf(Fout, Line2, &ArrayEntries[k][1]);
  212.                 }
  213.                 k += EntriesRowBased ? 1 : NumLines;
  214.             }
  215.             fprintf(Fout, "\n");
  216.             }
  217.             fprintf(Fout, "\n");
  218.             break;
  219.         case LATEX_DOCS:
  220.             fprintf(Fout,
  221.                 "\n\n\\smallskip\n\n\\begin{center}\n    \\begin{tabular}{|");
  222.             if (VertSep)
  223.             for (i = 0; i < NumPerLine; i++)
  224.                 fprintf(Fout, "|l");
  225.             else {
  226.             fprintf(Fout, "|");
  227.             for (i = 0; i < NumPerLine; i++)
  228.                 fprintf(Fout, "l");
  229.             }
  230.             fprintf(Fout,"||} \\hline \\hline\n");
  231.  
  232.             for (i = 0; i < NumLines; i++) {
  233.             k = EntriesRowBased ? i * NumPerLine : i;
  234.             fprintf(Fout, "\t");
  235.             for (j = 0; j < NumPerLine; j++) {
  236.                 fprintf(Fout, "%s %s ",
  237.                     k < NumArrayEntries ? &ArrayEntries[k][1] : "",
  238.                     j == NumPerLine - 1 ? "\\\\\n" : "&");
  239.                 k += EntriesRowBased ? 1 : NumLines;
  240.             }
  241.             }
  242.             fprintf(Fout,
  243.                 "\t\\hline\n    \\end{tabular}\n\\end{center}\n\n\\smallskip\n\n");
  244.             break;
  245.         case IRIT_HELP_DOCS:
  246.             sprintf(Line2, "%%-%ds", Indentation);
  247.             fprintf(Fout, "\n");
  248.             for (i = 0; i < NumLines; i++) {
  249.             k = EntriesRowBased ? i * NumPerLine : i;
  250.             fprintf(Fout, "   ");
  251.             for (j = 0; j < NumPerLine; j++) {
  252.                 if (k >= NumArrayEntries)
  253.                 fprintf(Fout, Line2, "");
  254.                 else {
  255.                 FilterLatexMods(ArrayEntries[k],
  256.                         ArrayEntries[k]);
  257.                 fprintf(Fout, Line2, &ArrayEntries[k][1]);
  258.                 }
  259.                 k += EntriesRowBased ? 1 : NumLines;
  260.             }
  261.             fprintf(Fout, "\n");
  262.             }
  263.             fprintf(Fout, "\n");
  264.             break;
  265.         case UNDEFINED_DOC_PROCESS:
  266.             break;
  267.         }
  268.  
  269.         continue;
  270.     }
  271.  
  272.     switch (OutputDocType) {
  273.         case PLAIN_TEXT_DOCS:
  274.         switch (Line1[0]) {
  275.             case '@':
  276.             case '$':
  277.             break;
  278.             case '*':
  279.             fprintf(Fout, "%s", &Line1[1]);
  280.             break;
  281.             case '&':
  282.             case '!':
  283.                 j = (78 - (int) strlen(&Line1[1])) / 2;
  284.                 for (i = 0; i < j; i++)
  285.                 fprintf(Fout, " ");
  286.             fprintf(Fout, "%s", &Line1[1]);
  287.                 for (i = 0; i < j; i++)
  288.                 fprintf(Fout, " ");
  289.             for (i = 1; i < (int) strlen(&Line1[1]); i++)
  290.                 fprintf(Fout, "-");
  291.             fprintf(Fout, "\n");
  292.             break;
  293.             default:
  294.             fprintf(stderr,
  295.                 "Undefined first char command at line %d.\n",
  296.                 LineNum);
  297.             /* Let it print without the first char... */
  298.             case 0:
  299.             case 10:
  300.             case 13:
  301.             case ' ':
  302.             FilterLatexMods(Line1, Line2);
  303.             fprintf(Fout, "%s", &Line2[1]);
  304.             break;
  305.         }
  306.         break;
  307.         case LATEX_DOCS:
  308.         switch (Line1[0]) {
  309.             case '!':
  310.             case '$':
  311.             case '&':
  312.             break;
  313.             case '@':
  314.             case '*':
  315.             fprintf(Fout, "%s", &Line1[1]);
  316.             break;
  317.             default:
  318.             fprintf(stderr,
  319.                 "Undefined first char command at line %d.\n",
  320.                 LineNum);
  321.             /* Let it print without the first char... */
  322.             case ' ':
  323.             case 0:
  324.                 case 10:
  325.             case 13:
  326.             fprintf(Fout, "%s", Line1[1] == 0 ? "\n" : &Line1[1]);
  327.             break;
  328.         }
  329.         break;
  330.         case IRIT_HELP_DOCS:
  331.         switch (Line1[0]) {
  332.             case '@':
  333.             case '&':
  334.             break;
  335.             case '*':
  336.             fprintf(Fout, "%s", &Line1[1]);
  337.             break;
  338.             case '!':
  339.             fprintf(Fout, "%s",  &Line1[1]);
  340.             break;
  341.             case '$':
  342.             fprintf(Fout, "$\n");
  343.             break;
  344.             default:
  345.             fprintf(stderr,
  346.                 "Undefined first char command at line %d.\n",
  347.                 LineNum);
  348.             /* Let it print without the first char... */
  349.             case ' ':
  350.             case 0:
  351.             case 10:
  352.             case 13:
  353.             FilterLatexMods(Line1, Line2);
  354.             fprintf(Fout, "%s", &Line2[1]);
  355.             break;
  356.         }
  357.         break;
  358.         case UNDEFINED_DOC_PROCESS:
  359.         break;
  360.     }
  361.     }
  362. }
  363.  
  364. /*****************************************************************************
  365. * Filters out expression of the form '{\cmd * }' in Line1 into '*' in line2. *
  366. * These are latex modifiers not wanted in plain text output.             *
  367. *   Also are filtered out any \? to ?, any $*$ to * and \verb+^+ to ^.       *
  368. *   A space is forced as first char in line.                     *
  369. *****************************************************************************/
  370. void FilterLatexMods(char *Line1, char *Line2)
  371. {
  372.     int i = 0,
  373.     j = 0,
  374.     InModifier = FALSE;
  375.     char Line3[LINE_LEN_LONG];
  376.  
  377.     Line3[j++] = ' ';
  378.  
  379.     while (Line1[i]) {
  380.     if (Line1[i] == '{' && Line1[i + 1] == '\\' && !isspace(Line1[i + 2])) {
  381.         /* Here is one - filter it out. */
  382.         while (!isspace(Line1[i++]));
  383.         while (isspace(Line1[i++]));
  384.         i--;
  385.         InModifier = TRUE;
  386.     }
  387.     else if (Line1[i] == '}' && InModifier) {
  388.         i++;
  389.         InModifier = FALSE;
  390.     }
  391.     else if (strncmp(&Line1[i], "\\verb+^+", 8) == 0) {
  392.         i += 8;
  393.         Line3[j++] = '^';
  394.     }
  395.     else if (Line1[i] == '\\') {
  396.         i++;
  397.         Line3[j++] = Line1[i++];
  398.     }
  399.     else if (Line1[i] == '$' && i > 0) {
  400.         i++;
  401.     }
  402.     else
  403.         Line3[j++] = Line1[i++];
  404.     }
  405.     Line3[j] = 0;
  406.     strcpy(Line2, Line3);
  407. }
  408.  
  409.